package com.bilibili.server.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bilibili.commons.Constants;
import com.bilibili.commons.exception.TanHuaException;
import com.bilibili.model.vo.ErrorResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigInteger;

/**
 * @author HuangRongFu
 * 2021/11/29 星期一8:09 下午
 */

@Service
public class UserFreezeService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     *
     * @param state 1：登录时检查， 2：点赞，评论，喜欢 检查, 3. 发布动态、视频 检查
     * @param phone 登录用户id
     */
    public void checkUserStatus(Integer state,String phone) {

        //用户被冻结了
        String value = stringRedisTemplate.opsForValue().get(Constants.FREEZE_USER + phone);

        JSONObject jsonObject = JSON.parseObject(value);

        if (StringUtils.isNotEmpty(value)) {
            //取出冻结信息
            Integer freezingRange = jsonObject.getInteger("freezingRange");

            if (freezingRange == state) {

                // 用户的这个功能被冻结,不能使用这个功能
                throw new TanHuaException(ErrorResult.builder().errMessage("您的账号被冻结！原因:"
                        + jsonObject.getString("reasonsForFreezing")).build());
            }
        }
    }
}
